package com.hctforgreen.greenservice.utils;


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


/**
 * 可以处理中文文件名
 */
public class UnZipUtil 
{
	private static final int buffer = 2048;

//	public static void main(String[] args)
//	{
//		unZip("D:\\ss\\test.zip");
//	}

	public static void unZip(String path)
	{
		int count = -1;
		int index = -1;
		String savepath = "";
		boolean flag = false;

		File file = null; 
		InputStream is = null;  
		FileOutputStream fos = null;  
		BufferedOutputStream bos = null;

		savepath = path.substring(0, path.lastIndexOf("\\")) + "\\";

		try
		{ 
			ZipFile zipFile = new ZipFile(path); 

			Enumeration<?> entries = zipFile.entries();

			while(entries.hasMoreElements())
			{ 
				byte buf[] = new byte[buffer]; 

				ZipEntry entry = (ZipEntry)entries.nextElement(); 

				String filename = entry.getName();
				index = filename.lastIndexOf("/");
				if(index > -1)
					filename = filename.substring(index+1);

				filename = savepath + filename;

				flag = isPics(filename);
				if(!flag)
					continue;

				file = new File(filename); 
				file.createNewFile();

				is = zipFile.getInputStream(entry); 

				fos = new FileOutputStream(file); 
				bos = new BufferedOutputStream(fos, buffer);

				while((count = is.read(buf)) > -1)
				{ 
					bos.write(buf, 0, count ); 
				} 

				fos.close(); 

				is.close(); 
			} 

			zipFile.close(); 

		}catch(IOException ioe){ 
			ioe.printStackTrace(); 
		} 
	} 

	public static boolean isPics(String filename)
	{
		boolean flag = false;

		if(filename.endsWith(".jpg") || filename.endsWith(".gif")  || filename.endsWith(".bmp") || filename.endsWith(".png"))
			flag = true;

		return flag;
	}
	/**
	 * 获取解压后的路径
	 * @param filePath
	 * @return
	 */
	public static String getDeCompressionFilePath(String filePath){
		return filePath.replace(".zip", "");
	}
	/**
	 * 解压zip文件
	 * @param filePath 需要解压的zip文件路径(可以是文件/文件夹)      
	 * @param decompressionPath  为解压后文件的存放路径       
	 */
	public static void deCompressionZipFile(String filePath, String decompressionPath)throws IOException{
		//验证ZIP压缩文件是否有效
		File file = new File(filePath);
		boolean exists = file.exists();//判断文件/文件夹是否存在
		if (exists) {
			//构造目录文件对象
			File dirs = new File(decompressionPath);
			if (!dirs.exists()) {//如果目录不存在,创建decompressionPath目录
				dirs.mkdirs();
			}
			ZipFile zipFile = new ZipFile(filePath);
			Enumeration<?> enumeration = zipFile.entries();
			while (enumeration.hasMoreElements()) {
				ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
				String entryName = zipEntry.getName().replace(File.separatorChar,'/');
				String[] names = entryName.split("/");
				int length = names.length;
				String path = decompressionPath;
				for (int v = 0; v < length; v++) {
					if(v <length-1) {
						if(v == 0){
							path+= "/"+ names[v]+"/";
						}else{
							path+= names[v] + "/";
						}
						
						new File(path).mkdir();
					} else { //最后一个
						if (entryName.endsWith("/")) { //为目录,则创建文件夹
							new File(decompressionPath + "/" + entryName).mkdir();
						} else {
							InputStream in = zipFile.getInputStream(zipEntry);
							OutputStream os = new FileOutputStream(new File(decompressionPath +"/"+ entryName));
							byte[] buffer = new byte[1024];
							int len = 0;
							while ((len = in.read(buffer)) > 0) {
								os.write(buffer, 0, len);
							}
							in.close();
							os.close();
						}
					}
				}
			}
			zipFile.close();
			//删除压缩文件
			file.delete();
		} else {
			throw new IOException("要解压的文件/文件夹不存在!");
		}
	}
	
}

